<?php
/*
 * 商品控制器
 */
class GoodsController extends IsLoginController{
	private $Model;
	public function __init(){
		parent::__init();
		$this->Model = K('Goods');
	}
	//商品管理首页
	public function index(){
		//查询数据分配到视图
		$goodsData = M('goods')->where('goods_recycle =0')->all();
		$this->assign('goodsData',$goodsData);
		$this->display();
	}
	//添加商品
	public function add(){
		header("Content-type:text/html;charset=utf-8");
		if(IS_POST){
			//goodsModel 添加方法 添加商品
			if(!$goodBackInfo = $this->Model->addGoods()){
				$this->error($this->Model->error);
			}
			//将返回商品id 记录session 其他Model调用			
			$_SESSION['goods_gid'] = $goodBackInfo;	
			
			//商品详情表添加
			$Info =K('GoodsInfo');
			if(!$Info->addGoodsInfo()){
				$this->error($Info->error);
			}

			//商品属性表添加
			$Attr =K('goodsAttr');
			if(!$Attr->addGoodsAttr()){
				$this->error($Attr->error);
			}
			
			$this->success('商品添加成功');
		}
		//查询分类信息
		$cateInfo = Data::tree(M("category")->all(),'category_name');
		$this->assign('cateInfo',$cateInfo);
		//查询品牌
		$brandInfo = K("brand")->all();
		$this->assign('brandInfo',$brandInfo);

		
		$this->display();
	}
	//编辑商品
	public function edit(){
		header("Content-type:text/html;charset=utf-8");
		if(IS_POST){
			$gid = Q('get.gid');
			//goodsModel 添加方法 添加商品
			if(!$goodBackInfo = $this->Model->editGoods($gid)){
				$this->error($this->Model->error);
			}
			//将返回商品id 记录session 其他Model调用			
			$_SESSION['goods_gid'] = $goodBackInfo;	
			
			//商品详情表添加
			$Info =K('GoodsInfo');
			if(!$Info->editGoodsInfo($gid)){
				$this->error($Info->error);
			}

			//商品属性表添加
			$Attr =K('goodsAttr');
			if(!$Attr->editGoodsAttr($gid)){
				$this->error($Attr->error);
			}
			
			$this->success('商品修改成功');

		}
		//查询数据 分配视图  关联3张表 商品表 goods 商品属性表 goods_attr 商品详情表 goods_info
		$gid = Q('get.gid');
		//判断当前商品有没有商品属性 如果没有不查询规格属性 如果有就查询  避免编辑无信息
		$goodsBool = M('goods_attr')->where("goods_gid ='{$gid}'")->find();
		if($goodsBool){
			$goodsData = M()->JOIN("__goods__ g JOIN __goods_attr__ attr JOIN __goods_info__ info ON g.gid = attr.goods_gid AND g.gid= info.goods_gid")->where("gid ='{$gid}'")->find();
			//查询商品属性、商品规格
			$tid=$goodsData['type_tid'];
			$typeAttr = K('type_attr')->where("type_tid='{$tid}'")->all();
			foreach($typeAttr as $k =>$v){
				$typeAttr[$k]['attrValue'] = explode(',', $v['attr_value']);
			}
			//分配商品属性、商品规格
			$this->assign('typeAttr',$typeAttr);
		}else{
			$goodsData = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("gid ='{$gid}'")->find();
		}
		
		$this->assign('goodsData',$goodsData);
//		var_dump($goodsData);die;
		//查询分类信息
		$cateInfo = Data::tree(M("category")->all(),'category_name');
		$this->assign('cateInfo',$cateInfo);
		//查询品牌
		$brandInfo = K("brand")->all();
		$this->assign('brandInfo',$brandInfo);
		
		
		
		//查询商品属性 返回数组
		$GoodsAttrInfo = M('goods_attr')->where("goods_gid ='{$gid}'")->all();
		$this->assign('GoodsAttrInfo',$GoodsAttrInfo);
		
		//商品图片组合路径
		$imgInfo = array_filter(explode(',', $goodsData['goods_info_smallIMg']));
		$this->assign('imgInfo',$imgInfo);
		$this->display();
	}
	//回收站首页
	public function recycle(){
		$goodsData = M('goods')->where('goods_recycle =1')->all();
		$this->assign('goodsData',$goodsData);
		$this->display();
	}
	//回收站删除
	public function recycleDel(){
		if(IS_GET){
			$gid = Q('get.gid');
			//删除商品表 goods gid
			M('goods')->where("gid = '{$gid}'")->del();
			//删除商品属性表  goods_attr goods_gid
			M('goods_attr')->where("goods_gid = '{$gid}'")->del();
			//删除属性规格表 type_info goods_gid
			M('goods_info')->where("goods_gid = '{$gid}'")->del();
			//删除货品表 goods_list goods_gid
			M('goods_list')->where("goods_gid = '{$gid}'")->del();
			
			$this->success('删除成功');
		}
	}
	//删除到回收站
	public function del(){
		if(IS_GET){
			$gid = Q('get.gid'); 
			$data['goods_recycle']='1';
			$data  = $this->Model->where("gid ='{$gid}'")->update($data);
			$this->success('删除成功');
		}

	}
	//恢复回收站商品
	public function recover(){
		if(IS_GET){
			$gid = Q('get.gid'); 
			$data['goods_recycle']='0';
			$data  = $this->Model->where("gid ='{$gid}'")->update($data);
			$this->success('恢复成功');
		}

	}
	//商品图片上传  异步上传接口
	public function uploadGoodsImg(){
			$upload = new Upload('upload/goods/'. date('y/m/d'));
			$file = $upload->upload();
			
			//缩略图处理  一张图缩了三张图
			$img = new image();		
			$img->thumb($file[0]['path'],'./upload/goods/small/'.$file[0]['filename'].'_thumb_100X100.jpg',100,100,6);
			$img->thumb($file[0]['path'],'./upload/goods/middle/'.$file[0]['filename'].'_thumb_400X400.jpg',400,400,6);
			$img->thumb($file[0]['path'],'./upload/goods/big/'.$file[0]['filename'].'_thumb_800X800.jpg',800,800,6);
			
			$data = $file[0];
			$data['goods_info_smallIMg'] = $file[0]['filename'];
			$this->ajax($data);
	}
	
	//异步删除图片
	public function delImg(){
		if(IS_AJAX){
			//获取异步图片名
			$filename = Q('post.filename');
			//组合文件路径
			$filenameBig = "./upload/goods/big/".$filename."_thumb_800X800.jpg";
			$filenameSmall = "./upload/goods/small/".$filename."_thumb_100X100.jpg";
			$filenameMiddle = "./upload/goods/middle/".$filename."_thumb_400X400.jpg";
			//删除文件
			unlink($filenameBig);unlink($filenameSmall);unlink($filenameMiddle);
			echo 1;
		}
	}
	
	//异步查询商品属性 、规格
	public function getGoodsAttr(){
		if(IS_AJAX){
			$tid=Q('post.tid',0,'intavl');
			$typeAttr = K('type_attr')->where("type_tid='{$tid}'")->all();
			foreach($typeAttr as $k =>$v){
				$typeAttr[$k]['attrValue'] = explode(',', $v['attr_value']);
			}	
			$this->ajax($typeAttr);
		
		}
	}
	
	public function test(){
		if(IS_POST){
			$upload = new Upload('upload/goods/'. date('y/m/d'));
			$thumb = new WaterMark();			
			$file = $upload->upload();
			p($file);
			foreach($file as $k => $v){
				//缩略图处理 大中小图
				$smallImg[$k] = $thumb->ThumImg($v['path'],200,200,'yes','./upload/goods/small',$v['filename']);
				$bigImg[$k] = $thumb->ThumImg($v['path'],800,800,'yes','./upload/goods/big',$v['filename']);
				$middlImg[$k] = $thumb->ThumImg($v['path'],400,400,'yes','./upload/goods/middle',$v['filename']);
				$data = $file[0];
				//返回数据用 
				$data['s_thumb'] = $smallImg;	
				$data['b_thumb'] = $bigImg;	
				$data['m_thumb'] = $middlImg;
				p($data);
				//记录数据 插入数据库
				//原图路径形式  http://localhost/0423/suning/upload/goods/15/06/25/19231435235850.jpg  未作删除
				//缩略图路径形式  只取文件名进行保存 
				//  [s_thumb] => Array
				//      (
				//          [0] => 	./upload/goods/small/90151435240047_thumb_200X200.jpeg
				//          [1] =>  ./upload/goods/middle/90151435240047_thumb_400X400.jpeg
				//          [2] =>  ./upload/goods/big/90151435240047_thumb_800X800.jpeg
				//      )
				$imgurl[] = $v['filename'];
			}
				//将文件名组合成字符串','号分隔开 直接插入数据库 调用是 组合数据
				$imgurl = implode(',', $imgurl);
				//大中小选择一插入字段信息 只用插入一次
				$data['good_info_smallIMg'] = $imgurl;
		}
		$this->display();
	}
}
